//注册界面需求分析
//1.获取数据判断用户是否已注册
//1.1获取mock.json中users中数据
//1.2对数据进行遍历后进行判断
//1.3true已经注册false成功注册并且跳转
//2.注册成功跳转登录界面
window.onload = function() {
	var phoneNumer = document.querySelectorAll("input")[0]
	var password = document.querySelectorAll("input")[1]
	var button = document.querySelector('.res')

	button.onclick = function addUsers() {

		//获取用户的id 获取对应的shopId
		//查询对应的json数据里面有没有这个用户
		console.log("asda");
		var phoneNum = phoneNumer.value
		var pwd = password.value
		var pattern2 = /.{8,16}/
		var pattern3 = /\d{11}/
		if (!pattern3.test(phoneNum)) {
			alert("手机号不合法!")
			return false
		}
		if (!pattern2.test(pwd)) {
			alert("密码长度在8到16位!")
			return false
		}
		$.ajax({
			type: "get",
			url: `http://localhost:9900/users?username=${phoneNum}`,
			dataType: "json",
			success: function(response) {
				console.log(response);
				if (response.length == 0) { //用户未注册
				alert("注册成功,请登录");
					$.ajax({
						type: "post",
						url: "http://localhost:9900/users",
						data: JSON.stringify({
							id: Date.now(), //利用时间挫完成对应的id添加操作 购物车id
							username: phoneNum,
							password: pwd
						}),
						dataType: "json",
						contentType: "application/json",
						success: function(response) {
							console.log('成功');
						}
					})
					window.location
						.href =
						"./login.html";
					console.log("用户未注册");
				} else { //用户已注册
					alert("用户已注册,请直接登录")
					window.location
						.href =
						"./login.html";
					console.log("用户已注册");
				}
			}
		});
	}



}
